<?php

/**
 * 文件处理
 *
 * Author: Surfin
 * Datetime: 16/10/20 下午2:21
 */

namespace Services;

use Symfony\Component\DependencyInjection\ContainerInterface;

class Files
{
 
    private $container;
    
    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }


    /**
     * 获取数据源
     *
     * @param $fileName
     * @return mixed
     */
    public function readToJson($fileName)
    {
        $file = fopen( dirname( $this->container->get('kernel')->getRootDir() ). DIRECTORY_SEPARATOR .$fileName, 'r');
        $data = fread($file, filesize($fileName));
        $data = json_decode($data, true);
        fclose($file);

        return $data;
    }


    /**
     * 上传附件
     *
     * @param $inputName
     * @param string $path
     * @param string $filename
     * @param array $mineType
     * @param int $size
     * @return array
     */
    public function upload($inputName, $path = 'upload', $filename = '', $mineType = array('image/png', 'image/gif', 'image/jpg', 'image/jpeg'), $size = 2)
    {

        $result = array('errorCode' => 1);

        $path_name = date('Y-m');
        $path = $path . DIRECTORY_SEPARATOR . $path_name;

        if (!file_exists($path)) {
            mkdir($path, 0755);
        }

        $storage = new \Upload\Storage\FileSystem($path);
        $file = new \Upload\File($inputName, $storage);

        // Optionally you can rename the file on upload

        $filename = $filename ?: uniqid();
        $file->setName($filename);

        // Validate file upload
        // MimeType List => http://www.iana.org/assignments/media-types/media-types.xhtml
        $file->addValidations(array(
            // Ensure file is of type "image/png"
            new \Upload\Validation\Mimetype($mineType),

            //You can also add multi mimetype validation
            //new \Upload\Validation\Mimetype(array('image/png', 'image/gif'))

            // Ensure file is no larger than 5M (use "B", "K", M", or "G")
            new \Upload\Validation\Size($size . 'M')
        ));

        // Access data about the file that has been uploaded
        $data = array(
            'path_name' =>  $path_name . DIRECTORY_SEPARATOR . $file->getNameWithExtension(),
            'name'       => $file->getNameWithExtension(),
            'extension'  => $file->getExtension(),
            'mime'       => $file->getMimetype(),
            'size'       => $file->getSize(),
            'md5'        => $file->getMd5(),
            'dimensions' => $file->getDimensions()
        );

        // Try to upload file
        try {
            // Success!
            $file->upload();
            $result['errorCode']  = 0;
            $result['data'] = $data;
        } catch (\Exception $e) {
            // Fail!
            $errorMessages = $file->getErrors();
            array_unshift($errorMessages, '上传失败');
            $result['errorMessages'] = $errorMessages; //$errorMessages;
        }

        return $result;
    }

    /**
     * 上传图片
     *
     * @param $inputName
     * @param string $path
     * @param string $filename
     * @param array $mineType
     * @param int $size
     * @return array
     */
    public function uploadImage($inputName, $path = 'upload', $filename = '', $mineType = array('image/png', 'image/gif', 'image/jpg', 'image/jpeg'), $size = 2)
    {
        return $this->upload($inputName, $path, $filename, $mineType, $size);
    }

    /**
     * 删除文件 仅限upload内的文件
     *
     * @param $path
     */
    public function removeFile($path)
    {

    }
    
}
